package com.pca.headline.service.impl;

import com.pca.headline.dao.NewsHeadlineDao;
import com.pca.headline.dao.impl.NewsHeadlineDaoImpl;
import com.pca.headline.pojo.NewsHeadline;
import com.pca.headline.pojo.vo.HeadlineDetailVo;
import com.pca.headline.pojo.vo.HeadlinePageVo;
import com.pca.headline.pojo.vo.HeadlineQueryVo;
import com.pca.headline.service.NewsHeadlineService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewsHeadlineServiceImpl implements NewsHeadlineService {
    private NewsHeadlineDao headlineDao = new NewsHeadlineDaoImpl();
    /**
     * pageInfo{
     *     pageData[
     *         {
     *               Integer hid;
     *               String title;
     *               Integer type;
     *               Integer pageViews;
     *               Long pastHours;
     *               Integer publisher;
     *         },
     *  pageNum //页码数
     *  pageSize //页大小
     *  totalPage //总页数
     *  totalSize //总记录数
     *     ]
     * }
     * @param headlineQuery
     * @return
     */
    @Override
    public Map findPage(HeadlineQueryVo headlineQuery) {
        int pageNum = headlineQuery.getPageNum();
        int pageSize = headlineQuery.getPageSize();
        List<HeadlinePageVo> pageData = headlineDao.findPageList(headlineQuery);
        //int totalSize = pageData.size();
        int totalSize = headlineDao.findCount(headlineQuery);
        int totalPage = totalSize % pageSize == 0 ? totalSize / pageSize : totalSize / pageSize + 1;
        //上面不行list只加了至多pageSize
        Map pageInfo = new HashMap();
        pageInfo.put("pageData",pageData);
        pageInfo.put("pageNum",pageNum);
        pageInfo.put("pageSize",pageSize);
        pageInfo.put("totalSize",totalSize);
        pageInfo.put("totalPage",totalPage);
        return pageInfo;

    }

    @Override
    public HeadlineDetailVo findHeadlineDetail(int hid) {
        int row = headlineDao.incrPageViews(hid);
        return headlineDao.findHeadlineDetail(hid);
    }

    @Override
    public int addNewsHeadline(NewsHeadline newsHeadline) {
        return headlineDao.addNewsHeadline(newsHeadline);
    }

    @Override
    public NewsHeadline findByHid(int hid) {
        return headlineDao.findByHid(hid);
    }

    @Override
    public int updateNewsHeadline(NewsHeadline headline) {
        return headlineDao.update(headline);
    }

    @Override
    public int removeByHid(int hid) {
        return headlineDao.removeByHid(hid);
    }
}
